package com.linuxgt.android.dbapp.service;

import android.support.test.InstrumentationRegistry;
import android.util.Log;

import com.linuxgt.android.dbapp.domain.User;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.util.List;

import static org.junit.Assert.*;

/**
 * Created by liuteng on 2017/5/17.
 */
public class VIPUserServiceTest {

    private VIPUserService vipUserService;

    @Before
    public void init() throws Exception {
        vipUserService = new VIPUserService(InstrumentationRegistry.getTargetContext());
    }


    @Test
    public void save() throws Exception {
        User user = new User("test", 99);
        vipUserService.save(user);
    }

    @Test
    public void delete() throws Exception {
        User user = vipUserService.query(99);
        if (null == user) {
            vipUserService.save(new User(99, "test", 99));
            vipUserService.delete(99);
        } else {
            vipUserService.delete(99);
        }
    }

    @Test
    public void query() throws Exception {
        User user = vipUserService.query(99);
        if (null == user) {
            vipUserService.save(new User(99, "test", 99));
            user = vipUserService.query(99);
        }
        Log.i(getClass().getSimpleName(), user.toString());
    }

    @Test
    public void getPageData() throws Exception {
        List<User> users = vipUserService.getPageData(1, 10);
        for (User user : users) {
            Log.i(getClass().getSimpleName(), user.toString());
        }
    }

    @Test
    public void update() throws Exception {
        User user = vipUserService.query(1);
        if (null == user) {
            user = new User(1, "liuteng", 0);
        }
        user.setAge(user.getAge() + 1);
        vipUserService.update(user);
        Log.i(getClass().getSimpleName(), user.toString());

    }

    @Test
    public void getCount() throws Exception {
        Log.i(getClass().getSimpleName(), String.valueOf(vipUserService.getCount()));
    }


    @After
    public void clear() throws Exception {
        long count = vipUserService.getCount();
        if (count > 50) {
            Log.i(getClass().getSimpleName(), String.valueOf(count));
        }
    }
}